package main;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Line Processor is a singleton class
 * @author luisp
 *
 */
public class LineProcessor {

	private List<RegexpTranslation> conditions = new ArrayList<RegexpTranslation>();
	
	private static LineProcessor instance = null;
	
	private LineProcessor(){
		
		//First Order
		//$(elementId) -> $('#elementId')
		conditions.add(new RegexpTranslation("(?:[^$]+|^)\\$\\(['\"]([^'\"]*)['\"]\\)", "jQuery('#$1')", 1));
		//$$(selector) -> $(selector)
		conditions.add(new RegexpTranslation("\\$\\$\\(([^)]+)\\)", "jQuery($1)", 1));
		conditions.add(new RegexpTranslation(".select\\(", ".find(", 1));
		
		//Second Order
		
		conditions.add(new RegexpTranslation("\\$F\\(['\"]([^)]*)['\"]\\)", "jQuery('#$1').val()", 2));
		conditions.add(new RegexpTranslation("\\$\\(([^)]*)\\).getValue\\(\\)", "jQuery($1).val()", 2));
		conditions.add(new RegexpTranslation("\\$\\(([^)]*)\\).innerHTML\\s*=\\s*([^,]*);", "jQuery($1).html($2);", 2));
		
		//Third order

		conditions.add(new RegexpTranslation(".[sg]etStyle\\(", ".css(", 3));
		conditions.add(new RegexpTranslation(".classNames\\(\\)", ".attr('className')", 3));
		conditions.add(new RegexpTranslation(".hasClassName\\(", ".hasClass(", 3));
		conditions.add(new RegexpTranslation(".toggleClassName\\(", ".toggleClass(", 3));
		conditions.add(new RegexpTranslation(".addClassName\\(", ".addClass(", 3));
		conditions.add(new RegexpTranslation(".removeClassName\\(", ".removeClass(", 3));
		
		//Fourth order
		
		conditions.add(new RegexpTranslation(".(?:read|write|set)Attribute\\(", ".attr(", 4));
		conditions.add(new RegexpTranslation(".getWidth\\(\\)", ".width()", 4));
		conditions.add(new RegexpTranslation(".getHeight\\(\\)", ".height()", 4));
		conditions.add(new RegexpTranslation(".empty\\(\\)", ".is(':empty')", 4));
		
		//Fifth order
		
		conditions.add(new RegexpTranslation(".observe\\(", ".bind(", 5));
		conditions.add(new RegexpTranslation(".observe\\('dom:loaded", "$(document).ready(", 5));

		//Sixth order
		
		conditions.add(new RegexpTranslation(".observe\\(", ".bind(", 6));
		conditions.add(new RegexpTranslation(".observe\\('dom:loaded", "jQuery(document).ready(", 5));

		//Seventh order
		
		conditions.add(new RegexpTranslation("new\\s*Element\\(", "jQuery(", 7));
		
		Collections.sort(conditions, new Comparator<RegexpTranslation>(){

			@Override
			public int compare(RegexpTranslation o1, RegexpTranslation o2) {
				if (o1.getOrder() < o2.getOrder()){
					return 1;
				}
				return -1;
			}
			
		});
	}
	
	/**
	 * Run all the conditions and translate the line
	 * @param line
	 * @return
	 */
	public String processLine(String line){
		String translatedLine = line;
		for (RegexpTranslation tuple : conditions) {
//			translatedLine = translatedLine.replaceAll("(?si)" + tuple.getRegexp(), tuple.getTranslation());
		}
		return translatedLine;
	}
	
	public static LineProcessor getInstance(){
		if (instance == null){
			instance = new LineProcessor();
		}
		return instance;
	}
	
}
